package com.company.nuwa.common.utils;

import com.company.nuwa.common.enumration.IBizCode;
import com.company.nuwa.common.exception.BizException;

import java.util.Collection;
import java.util.Objects;

/**
 * <p>preconditions</p >
 *
 * @author Mitchell
 * @version 1.0
 * @date 2020/05/28 13:07
 */
public interface Preconditions {

    int THRESHOLD_SIZE = 1000;

    /**
     * check argument
     *
     * @param expression
     * @param code
     * @param message
     */
    static void checkArgument(boolean expression, int code, String message) {
        if (!expression) {
            throw new BizException(code, message);
        }
    }

    /**
     * check argument
     *
     * @param expression
     * @param bizCode
     */
    static void checkArgument(boolean expression, IBizCode bizCode) {
        if (!expression) {
            throw new BizException(bizCode.getCode(), bizCode.getMessage());
        }
    }

    /**
     * check 集合阈值
     *
     * @param collection
     * @param bizCode
     */
    static <E> void checkThresholdSize(Collection<E> collection, IBizCode bizCode) {
        if (Objects.nonNull(collection) && collection.size() > THRESHOLD_SIZE) {
            throw new BizException(bizCode.getCode(), bizCode.getMessage());
        }
    }

    /**
     * check 集合阈值
     *
     * @param collection
     * @param code
     * @param message
     */
    static <E> void checkThresholdSize(Collection<E> collection, int code, String message) {
        if (Objects.nonNull(collection) && collection.size() > THRESHOLD_SIZE) {
            throw new BizException(code, message);
        }
    }

}
